package com.marvels.approve.common.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
import com.marvels.approve.common.dao.McamLoanApplyInfoCommonDao;
import com.marvels.approve.common.service.McamLoanApplyInfoCommonService;
import com.marvels.approve.consum.ApproveConsumerSystemApi;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.dto.McsmSystemParam;
import com.marvels.approve.mqueues.service.ApproveILoanApplyMqProduct;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.LoanApplyCaseLogDTO;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.SynLoanStatus2ApplyDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;

@Service
public class McamLoanApplyInfoCommonServiceImpl implements McamLoanApplyInfoCommonService {
	@Autowired
	private McamLoanApplyInfoCommonDao mcamLoanApplyInfoCommonDao;
	@Autowired
	private IMcamLoanApplyInfoDao mcamLoanApplyInfoDao;
	@Autowired
	private ApproveILoanApplyMqProduct loanApplyMqProduct;
	@Autowired
	private ApproveConsumerSystemApi consumerSystemApi;
	
	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	@Value("${config.properties.path}")
	private  String configPropertiesPath;
	
	@Override
	public int updateMcamLoanApplyInfoStatusByLoanId(String loanId, String status,String originalState, String updatedBy)throws MarvelsBusinessException {
		if(StringUtils.isEmpty(loanId) || StringUtils.isEmpty(status) ){
			throw new MarvelsBusinessException("入参均不能为空");
		}
		//如果送入的originalState为空，取数据库查询状态
		McamLoanApplyInfoDTO loanApply = mcamLoanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		if(org.apache.commons.lang.StringUtils.isEmpty(originalState)) {
			originalState = loanApply.getStatus();
		}
		//如果为送修改人默认去缓存中拿
		if(org.apache.commons.lang.StringUtils.isEmpty(updatedBy)) {
			updatedBy = CacheUtils.getCacheUser().getUserCode();
		}
		MarvelsLogUtil.getInstance().info(">>>>>loanId:"+loanId+",修改进件状态开始");
		int count = mcamLoanApplyInfoCommonDao.updateMcamLoanApplyInfoStatusByLoanId(loanId, status, updatedBy);
		//同步到进件系统中记录全流程状态
		String acLoanStatusFundList=ServerProperties.getValue(configPropertiesPath, "resource.properties", "acLoanStatus.fund.list");
		String acLoanStatusProductList=ServerProperties.getValue(configPropertiesPath, "resource.properties", "acLoanStatus.product.list");
		MarvelsLogUtil.getInstance().info(loanId+"同步到进件系统中记录全流程状态acLoanStatusFundList:"+acLoanStatusFundList+",acLoanStatusProductList:"+acLoanStatusProductList);
		if(count > 0  && (StringUtil.isEmpty(acLoanStatusProductList) || acLoanStatusProductList.indexOf(","+loanApply.getProductType()+",")==-1) ) {//占时停止
			SynLoanStatus2ApplyDTO synStatus = new SynLoanStatus2ApplyDTO();
			synStatus.setLoanId(loanId);
			synStatus.setStatus(status);
			synStatus.setFundCode(loanApply.getFundCode());
			synStatus.setFundName(loanApply.getFundName());
			synStatus.setOriginalState(originalState);
			synStatus.setPrincipalAmtAct(loanApply.getPrincipalAmtAct());
			loanApplyMqProduct.synLoanStatus2Apply(synStatus);
		}
//		if(status.equals("P05")){//处于放款状态
//			//先判断该笔进件是否满足自动放款条件
//			McsmSystemParam mcsmSystemParam=new McsmSystemParam();
//			mcsmSystemParam.setParamNames(new String[] { MarvelsConstant.AUTO_DF_FUNDS});
//			List<McsmSystemParam> params = consumerSystemApi.paramList(mcsmSystemParam);
//			if(null!=params && params.size()>0){
//				String paramValue = params.get(0).getParamValue();
//				String[] fundCodes = paramValue.split(",");
//				List<String> fundCodeList=Arrays.asList(fundCodes);
//				boolean isPull1=false;
//				for(String s:fundCodeList){
//					if(s.equals(loanId)){
//						isPull1=true;
//						break;
//					}
//				}
//				boolean isPull2=false;
//				//判断是否符合配置的产品
//				String productTypeConfig=ServerProperties.getValue(configPropertiesPath, "resource.properties", "auto.lend.product.type.list");
//				if(StringUtils.isNotEmpty(productTypeConfig)){
//					List<String> list=Arrays.asList(productTypeConfig.split(","));
//					for(String s:list){
//						if(s.equals(loanApply.getProductType())){
//							isPull2=true;
//							break;
//						}
//					}
//				}else{//没有配置否符合配置的产品  默认所有的产品都推
//					isPull2=true;
//				}
//				//将待放款信息推送到财务系统自动放款
//				if(isPull2 && isPull1){
//					rabbitTemplate.convertAndSend("time_pull_wait_lend_info",loanId);
//				}
//			}else{
//				MarvelsLogUtil.getInstance().info(">>>>>>>>>>未配置自动放款资金源");
//			}
//		}
		return count;
	}
 

}
